package com.oceanread.cn.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.oceanread.cn.entity.Drillhole1;
import com.oceanread.cn.entity.Treelist1;
import com.oceanread.cn.entity.Treelist2;
import com.oceanread.cn.service.ITreelist1Service;
import com.oceanread.cn.service.ITreelist2Service;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/Tree")
@CrossOrigin()
@Api(value = "目录列表", tags = {"3-图层管理"})
public class TreelistController {

    @Autowired
    private ITreelist1Service iTreelist1Service;
    @Autowired
    private ITreelist2Service iTreelist2Service;

    /*前端系统*/
    /*获取总表*/
    @ApiOperation(value = "前端可视化 获取总表")
    @GetMapping("/getall")
    public List<Treelist1> getList() {
        return iTreelist1Service.getall();
    }

    @ApiOperation(value = "前端可视化 获取图层总表11111")
    @GetMapping("/getalltree")
    public JSONArray getalltree() {
        iTreelist1Service.getall();
        JSONArray jsonArray = new JSONArray();
        List<Treelist1> treelist1s = iTreelist1Service.getall();
        for (Treelist1 treelist1 : treelist1s) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", treelist1.getId());
            jsonObject.put("name", treelist1.getName());
            jsonObject.put("open", treelist1.getOpen());
            jsonObject.put("influencechild", treelist1.getInfluencechild());
            jsonObject.put("checked", treelist1.getChecked());
            jsonArray.add(jsonObject);
        }
        List<Treelist2> treelist2s=iTreelist2Service.getall();
        for (Treelist2 treelist2:treelist2s){
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("pId", treelist2.getpId());
            jsonObject.put("id", treelist2.getId());
            jsonObject.put("name", treelist2.getName());
            jsonObject.put("path", treelist2.getPath());
            jsonObject.put("type", treelist2.getType());
            jsonObject.put("typeroot", treelist2.getTyperoot());
            try {
                jsonObject.put("layers", Integer.parseInt(treelist2.getLayers()));
            } catch (Exception e) {
            }
            jsonObject.put("open", treelist2.getOpen());
            jsonObject.put("enablePickFeatures", treelist2.getEnablePickFeatures());
            jsonObject.put("influencechild", treelist2.getInfluencechild());
            jsonObject.put("checked", treelist2.getChecked());
            jsonObject.put("Lon", treelist2.getLon());
            jsonObject.put("Lat", treelist2.getLat());
            jsonObject.put("Height", treelist2.getHeight());
            jsonObject.put("heading", treelist2.getHeading());
            jsonObject.put("pitch", treelist2.getPitch());
            jsonObject.put("roll", treelist2.getRoll());
            jsonArray.add(jsonObject);
        }
        return jsonArray;
    }

    @ApiOperation(value = "前端可视化 根据父目录获取 信息")
    @GetMapping("/getall/{pId}")
    public JSONArray getall2(@PathVariable("pId") String pId) {
        String key = "pId";
        String value = pId;
        JSONArray jsonArray = new JSONArray();
        List<Treelist2> list = iTreelist2Service.query(key, value);
        for (Treelist2 treelist2 : list) {//遍历查询到的数据
            if (treelist2.getpId().equals(value)) {//找到第一层数据
//                JSONObject jsonObject = (JSONObject) JSONObject.toJSON(treelist2);
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("pId", treelist2.getpId());
                jsonObject.put("id", treelist2.getId());
                jsonObject.put("name", treelist2.getName());
                jsonObject.put("path", treelist2.getPath());
                jsonObject.put("type", treelist2.getType());
                jsonObject.put("typeroot", treelist2.getTyperoot());
                try {
                    jsonObject.put("layers", Integer.parseInt(treelist2.getLayers()));
                } catch (Exception e) {
                }
                jsonObject.put("open", treelist2.getOpen());
                jsonObject.put("enablePickFeatures", treelist2.getEnablePickFeatures());
                jsonObject.put("influencechild", treelist2.getInfluencechild());
                jsonObject.put("checked", treelist2.getChecked());
                jsonObject.put("Lon", treelist2.getLon());
                jsonObject.put("Lat", treelist2.getLat());
                jsonObject.put("Height", treelist2.getHeight());
                jsonObject.put("heading", treelist2.getHeading());
                jsonObject.put("pitch", treelist2.getPitch());
                jsonObject.put("roll", treelist2.getRoll());
                JSONArray jsonArray_22 = new JSONArray();
                for (Treelist2 treelist_22 : list) {
                    if (treelist_22.getpId().equals(treelist2.getId())) {//找到第二层数据
//                        JSONObject jsonObject_22 = (JSONObject) JSONObject.toJSON(treelist_22);
                        JSONObject jsonObject_22 = new JSONObject();
                        jsonObject_22.put("pId", treelist_22.getpId());
                        jsonObject_22.put("id", treelist_22.getId());
                        jsonObject_22.put("name", treelist_22.getName());
                        jsonObject_22.put("path", treelist_22.getPath());
                        jsonObject_22.put("type", treelist_22.getType());
                        jsonObject_22.put("typeroot", treelist_22.getTyperoot());
                        try {
                            jsonObject_22.put("layers", Integer.parseInt(treelist_22.getLayers()));
                        } catch (Exception e) {
                        }
                        jsonObject_22.put("open", treelist_22.getOpen());
                        jsonObject_22.put("enablePickFeatures", treelist_22.getEnablePickFeatures());
                        jsonObject_22.put("influencechild", treelist_22.getInfluencechild());
                        jsonObject_22.put("checked", treelist_22.getChecked());
                        jsonObject_22.put("Lon", treelist_22.getLon());
                        jsonObject_22.put("Lat", treelist_22.getLat());
                        jsonObject_22.put("Height", treelist_22.getHeight());
                        jsonObject_22.put("heading", treelist2.getHeading());
                        jsonObject_22.put("pitch", treelist2.getPitch());
                        jsonObject_22.put("roll", treelist2.getRoll());
                        jsonArray_22.add(jsonObject_22);
                        jsonObject.put("parts", jsonArray_22);
                    }
                }
                jsonArray.add(jsonObject);
            }
        }
        return jsonArray;
    }

    /*后台系统*/
    @GetMapping("/getList")
    public JSONObject getList(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                              @RequestParam(name = "size", required = true, defaultValue = "10") int size) {
        JSONObject object = new JSONObject();
        object.put("data", iTreelist1Service.getTablelist(page, size));
        object.put("total", iTreelist1Service.getnum());
        return object;
    }

    @GetMapping("/getList2")
    public JSONObject getList2(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                               @RequestParam(name = "size", required = true, defaultValue = "10") int size) {
        JSONObject object = new JSONObject();
        object.put("data", iTreelist2Service.getTablelist(page, size));
        object.put("total", iTreelist2Service.getnum());
        return object;
    }

    @PostMapping("/update1")
    public Integer update1(@RequestBody Treelist1 treelist1) {
        Integer SYSID = treelist1.getSysid();
        Treelist1 treelist0 = iTreelist1Service.equal("SYSID", String.valueOf(SYSID)).get(0);
        treelist0.setId(treelist1.getId());
        treelist0.setName(treelist1.getName());
        iTreelist1Service.update(treelist0);
        return 1;
    }

    @PostMapping("/updateSwitch")
    public void updateSwitch(@RequestBody Treelist1 treelist1) {
        System.out.println(treelist1);
        Integer SYSID = treelist1.getSysid();
        Treelist1 treelist0 = iTreelist1Service.equal("SYSID", String.valueOf(SYSID)).get(0);
        if (treelist1.getChecked() != null) {
            treelist0.setChecked(treelist1.getChecked());
            System.out.println(treelist0);
            iTreelist1Service.update(treelist0);
        } else if (treelist1.getOpen() != null) {
            treelist0.setOpen(treelist1.getOpen());
            System.out.println(treelist0);
            iTreelist1Service.update(treelist0);
        } else if (treelist1.getInfluencechild() != null) {
            treelist0.setInfluencechild(treelist1.getInfluencechild());
            System.out.println(treelist0);
            iTreelist1Service.update(treelist0);
        }
    }

    @PostMapping("/insert1")
    public Integer insert1(@RequestBody Treelist1 treelist1) {
        return iTreelist1Service.insert(treelist1.getId(), treelist1.getName(),
                treelist1.getOpen(), treelist1.getChecked(), treelist1.getInfluencechild());
    }

    @GetMapping("/delete1")
    public Integer delete1(@RequestParam(name = "SYSID") int sysid) {
        return iTreelist1Service.delete(sysid);
    }

    @PostMapping("/update2")
    public Integer update2(@RequestBody Treelist2 treelist2) {
        System.out.println(treelist2);
        if (treelist2.getOpen() == null) {
            treelist2.setOpen(false);
        }
        if (treelist2.getEnablePickFeatures() == null) {
            treelist2.setEnablePickFeatures(false);
        }
        if (treelist2.getInfluencechild() == null) {
            treelist2.setInfluencechild(false);
        }
        return iTreelist2Service.update(treelist2);
    }

    @PostMapping("/insert2")
    public Integer insert2(@RequestBody Treelist2 treelist2) {
        System.out.println(treelist2);
        return iTreelist2Service.insert(treelist2.getpId(), treelist2.getId(), treelist2.getName(), treelist2.getPath(),
                treelist2.getType(), treelist2.getTyperoot(), treelist2.getLayers(), treelist2.getOpen(),
                treelist2.getEnablePickFeatures(), treelist2.getInfluencechild(), treelist2.getChecked(),
                treelist2.getLon(), treelist2.getLat(), treelist2.getHeight(),treelist2.getHeading(),
                treelist2.getPitch(),treelist2.getRoll());
    }

    @PostMapping("/updateSwitch2")
    public void updateSwitch2(@RequestBody Treelist2 treelist2) {
        System.out.println(treelist2);
        Integer SYSID = treelist2.getSysid();
        Treelist2 treelist0 = iTreelist2Service.equal("SYSID", String.valueOf(SYSID)).get(0);
        if (treelist2.getChecked() != null) {
            treelist0.setChecked(treelist2.getChecked());
            iTreelist2Service.update(treelist0);
        }
        if (treelist2.getOpen() != null) {
            treelist0.setOpen(treelist2.getOpen());
            iTreelist2Service.update(treelist0);
        }
        if (treelist2.getInfluencechild() != null) {
            treelist0.setInfluencechild(treelist2.getInfluencechild());
            iTreelist2Service.update(treelist0);
        }
    }

    @GetMapping("/delete2")
    public Integer delete2(@RequestParam(name = "SYSID") int SYSID) {
        return iTreelist2Service.delete(SYSID);
    }

    @GetMapping("/getList2query")
    public JSONObject getList2query(@RequestParam(name = "page", required = true, defaultValue = "1") int page,
                                    @RequestParam(name = "size", required = true, defaultValue = "10") int size,
                                    @RequestParam(name = "key") String key,
                                    @RequestParam(name = "value") String value
    ) {
        JSONObject object = new JSONObject();
        object.put("data", iTreelist2Service.getTablelistquery(key, value, page, size));
        object.put("total", iTreelist2Service.getnumquery(key, value));
        return object;
    }


}
